package alipay

import (
	"github.com/gin-gonic/gin"
	"github.com/gogap/logs"
	"yundian/internel/app/dao"
	"yundian/internel/app/helper"
	"yundian/internel/app/helper/request"
	"yundian/internel/app/logic"
	"yundian/internel/app/models/prices"
	"yundian/internel/pkg/dingo"
	. "yundian/internel/pkg/dingo/result"
)

// GetDevInfo 获取设备的详情信息
func GetDevInfo(ctx *gin.Context) {
	param := &struct {
		Sn string `form:"deviceSn" binding:"required"`
	}{}
	dingo.ErrorIf(ctx.ShouldBindQuery(param)).Unwrap()
	devInfo := logic.NewDeviceLogic().GetDeviceInfoByDeviceSn(param.Sn)
	Response(ctx)("", CodeSuccess, devInfo)(OK)
}

func GetMonthlyDevicePasswordTem(ctx *gin.Context) {
	userId, err := helper.GetUserIdFromCtx(ctx)
	dingo.ErrorIf(err).Unwrap()

	param := &request.GetZfbPasswordReq{}
	dingo.ErrorIf(ctx.ShouldBindJSON(param)).Unwrap()

	orderResult := dao.NewOrderAutoRenewDao().GetAutoRenewMonthly(userId, false)
	orderInfo := orderResult.Unwrap().(*request.GetAutoRenewMonthlyReq)
	if orderResult.Err != nil {
		Response(ctx)("", CodeInternalServerError, nil)(OK)
		return
	}

	if orderInfo.Id == 0 || orderInfo.Status != helper.TypeNumOrderMonthlyStatusCompletion {
		Response(ctx)("", CodeError, nil)(OK)
		return
	}

	pwdData := logic.NewDeviceLogic().HandleGetPasswordWxCoupon(param.DeviceSn, 60*1)
	Response(ctx)("", CodeSuccess, map[string]interface{}{
		"password": pwdData,
	})(OK)
}

// GetMonthlyDevicePassword 获取设备密码
func GetMonthlyDevicePassword(ctx *gin.Context) {
	userId, err := helper.GetUserIdFromCtx(ctx)
	dingo.ErrorIf(err).Unwrap()

	param := &request.GetZfbPasswordReq{}
	dingo.ErrorIf(ctx.ShouldBindJSON(param)).Unwrap()

	orderResult := dao.NewOrderAutoRenewDao().GetAutoRenewMonthly(userId, true)
	orderInfo := orderResult.Unwrap().(*request.GetAutoRenewMonthlyReq)
	if orderResult.Err != nil {
		Response(ctx)("", CodeInternalServerError, nil)(OK)
		return
	}

	if orderInfo.Id == 0 {
		Response(ctx)("", CodeError, nil)(OK)
		return
	}
	priceInfo := dao.NewPricesDao().GetPriceDetail(orderInfo.ProductId).Unwrap().(*prices.PriceModel)
	pwdData := logic.NewDeviceLogic().HandleGetPasswordWxCoupon(param.DeviceSn, int64(priceInfo.UseDuration))
	logs.Info("打印测试==============>", userId, orderInfo.ProductId, param.DeviceSn, orderInfo.DeviceSn)
	Response(ctx)("", CodeSuccess, map[string]interface{}{
		"password": pwdData,
	})(OK)
}

// GetDevicePassword 获取设备密码
func GetDevicePassword(ctx *gin.Context) {
	// 请求参数处理
	param := &request.GetZfbPasswordReq{}
	dingo.ErrorIf(ctx.ShouldBindJSON(param)).Unwrap()
	// 获取密码业务逻辑
	pwd, useTime := logic.NewDeviceLogic().HandleLogicGetPasswordZfb(param)

	Response(ctx)("", CodeSuccess, struct {
		Password string `json:"password"` // 密码
		UseTime  int64  `json:"useTime"`  // 使用时长
		Count    int    `json:"count"`    // 密码重置次数
	}{
		Password: pwd,
		UseTime:  useTime,
		Count:    0,
	})(OK)
}

// ResetDevPassword 重置设备密码
func ResetDevPassword(ctx *gin.Context) {
	// 请求参数处理
	data := &request.GetZfbPasswordReq{}
	dingo.ErrorIf(ctx.ShouldBindJSON(data)).Unwrap()
	// 重置密码业务逻辑
	pwd, useTime := logic.NewDeviceLogic().HandleLogicGetPasswordZfb(data)

	Response(ctx)("", CodeSuccess, struct {
		Password string `json:"password"`
		UseTime  int64  `json:"useTime"` // 使用时长
		Count    int    `json:"count"`   // 密码重置次数
	}{
		Password: pwd,
		UseTime:  useTime,
		Count:    0,
	})(OK)
}

// GetDeviceUseTime 获取设备使用时长
func GetDeviceUseTime(ctx *gin.Context) {
	// 请求参数处理
	param := &request.GetZfbPasswordReq{}
	dingo.ErrorIf(ctx.ShouldBindJSON(param)).Unwrap()
	_, useTime := logic.NewDeviceLogic().HandleAliPayUseTimeZfb(param)

	// 获取密码使用时长
	Response(ctx)("", CodeSuccess, useTime)(OK)
}
